package cn.tedu.javagame.star;

import javax.swing.*;
import java.awt.*;

public class Star {
    int x,y;
    Color currentColor;
    Color targetColor;
    int speed;
    String label="★";
    private static final int COLOR_STEP = 10; // 颜色变化步长

    public Star(){
        this.x = (int)(Math.random()*(StarFrame.WIDTH+1));
        this.y = (int)(Math.random()*(StarFrame.HEIGHT+1));
        this.currentColor = new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256));
        this.targetColor = getRandomColor();
        this.speed = (int)(Math.random()*4+3);
    }
    public Color getRandomColor() {
        return new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256));
    }
    public void move(){
        this.y += this.speed;
        // 计算颜色差值
        int redDiff = targetColor.getRed() - currentColor.getRed();
        int greenDiff = targetColor.getGreen() - currentColor.getGreen();
        int blueDiff = targetColor.getBlue() - currentColor.getBlue();

        // 计算颜色变化量
        int redStep = (int) Math.signum(redDiff) * Math.min(Math.abs(redDiff), COLOR_STEP);
        int greenStep = (int) Math.signum(greenDiff) * Math.min(Math.abs(greenDiff), COLOR_STEP);
        int blueStep = (int) Math.signum(blueDiff) * Math.min(Math.abs(blueDiff), COLOR_STEP);

        // 更新当前颜色
        int newRed = currentColor.getRed() + redStep;
        int newGreen = currentColor.getGreen() + greenStep;
        int newBlue = currentColor.getBlue() + blueStep;
        currentColor = new Color(newRed, newGreen, newBlue);

        // 如果颜色接近目标颜色，设置新的目标颜色
        if (Math.abs(redDiff) <= COLOR_STEP && Math.abs(greenDiff) <= COLOR_STEP && Math.abs(blueDiff) <= COLOR_STEP) {
            targetColor = getRandomColor();
        }
    }

}
